package com.fxwater.admin.controller.biz;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fxwater.biz.domain.FxwUserStationRelaInfo;
import com.fxwater.biz.domain.PumpStationInfo;
import com.fxwater.biz.domain.UnitInfo;
import com.fxwater.biz.service.IUserStationRelationService;
import com.fxwater.common.core.controller.BaseController;
import com.fxwater.common.core.domain.AjaxResult;
import com.fxwater.common.core.page.TableDataInfo;

/**
 * 用户和泵站关系管理
 * @author tx
 *
 */
@RequestMapping("/biz/stationUserRelation")
@Controller
public class UserStationRelationController extends BaseController {
	
	private static final Logger log = LoggerFactory.getLogger(UnitController.class);

	private String prefix = "biz/rela";

	@Autowired
	private IUserStationRelationService userStationRelationService;
	
	
	
	/**
	 * 增加管理单位
	 * 
	 * @param mmap
	 * @return
	 */
	@GetMapping("/add")
	public String add(String stationId, String stationName, ModelMap mmap) {
		mmap.put("stationId", stationId);
		mmap.put("stationName", stationName);
		return prefix + "/add";
	}
	
	/**
	 * 修改息状态
	 * @return
	 */
	@RequestMapping("/changeStatus")
	@ResponseBody
	@RequiresPermissions("biz:sur:edit")
	public AjaxResult changeStatus(String id,String status) {
		FxwUserStationRelaInfo userStationRelaInfo = new FxwUserStationRelaInfo();
		userStationRelaInfo.setId(Long.parseLong(id));
		userStationRelaInfo.setIsValid(status);
		return toAjax(userStationRelationService.editSaveStationUserRela(userStationRelaInfo));
	}
	
	@RequestMapping("/bindStationUsers")
	@ResponseBody
	public AjaxResult bindStationUsers(String stationId,String firstColumn) {
		userStationRelationService.bindStationUsers(stationId,firstColumn);
		return AjaxResult.success();
	}
	
	
	/**
	 * 修改息绑定状态
	 * @return
	 */
	@RequestMapping("/saveBindInfo")
	@ResponseBody
	@RequiresPermissions("biz:sur:edit")
	public AjaxResult saveBindInfo(String userId,String stationId) {
		FxwUserStationRelaInfo userStationRelaInfo = new FxwUserStationRelaInfo();
		userStationRelaInfo.setUserId(Long.parseLong(userId));
		userStationRelaInfo.setStationId(Long.parseLong(stationId));
		return toAjax(userStationRelationService.saveStationUserRela(userStationRelaInfo));
	}
	
	/**
	 * 跳转修改页面
	 * 
	 * @param mmap
	 * @return
	 */
	/*
	 * @GetMapping("edit/{id}") public String edit(ModelMap
	 * mmap, @PathVariable("id") String id) { PumpStationInfo pumpStationInfo = new
	 * PumpStationInfo(); pumpStationInfo.setId(Integer.parseInt(id));
	 * mmap.addAttribute("pumpStationInfo", unitInfo); return "/edit"; }
	 */
	
	/**
	 * 解绑
	 * @param ids
	 * @return
	 */
	@RequestMapping("/remove")
	@RequiresPermissions("biz:sur:remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(userStationRelationService.removeStationUserRela(ids));
	}
	
	/**
	 * 跳转页面
	 * 
	 * @return
	 */
	@RequiresPermissions("biz:unit:view")
	@GetMapping()
	public String pumpStation() {
		return prefix + "/list";
	}
	
	@GetMapping("/addUser")
	public String addUser(String stationId,String stationName,ModelMap mmap) {
		if(StringUtils.isEmpty(stationId)) {
			return prefix + "/add";
		}else {
			mmap.put("stationId", stationId);
			mmap.put("stationName", stationName);
			return prefix + "/detailAdd";
		}
		
	}
	
	
	

	@RequiresPermissions("biz:sur:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(FxwUserStationRelaInfo userStationRelaInfo) {
		startPage();
		List<FxwUserStationRelaInfo> list = userStationRelationService.selectFxwUserStationRelaInfoList(userStationRelaInfo);
		return getDataTable(list);
	}

	@RequiresPermissions("biz:sur:list")
	@PostMapping("/set/list")
	@ResponseBody
	public TableDataInfo setList(String stationId) {
		startPage();
		FxwUserStationRelaInfo userStationRelaInfo = new FxwUserStationRelaInfo();
		userStationRelaInfo.setStationId(Long.parseLong(stationId));
		List<FxwUserStationRelaInfo> list = userStationRelationService.selectFxwUserStationRelaInfoSetList(userStationRelaInfo);
		return getDataTable(list);
	}
	
	
	

}
